<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="5">
          <el-input placeholder="请输入用户名" clearable v-model="queryInfo.query" @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 数据展示列表 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="110px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="110px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180px">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormate}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220px">
          <template slot-scope="scope">
            <el-button type="warning" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGood(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="goodsHandleSizeChange" @current-change="goodsHandleCurrentChange" :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //获取商品列表的请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      //数据总条数
      total: 0,
      //商品列表数据
      goodsList: [],
    }
  },

  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('请求服务器失败！')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(res.data)
    },
    //pagesize变化时调用
    goodsHandleSizeChange(newSize){
        this.queryInfo.pagesize = newSize
        this.getGoodsList()
    },
    //pagenum变化时调用
    goodsHandleCurrentChange(newNum){
        this.queryInfo.pagenum = newNum
        this.getGoodsList()
    },
    //删除商品
    async deleteGood(goods_id){
        const confirmRes = await this.$confirm(
        '此操作将永久删除此商品信息, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((error) => error)
      if(confirmRes !== 'confirm') {
        return this.$message.info("取消了删除")
      }
      const {data: res} = await this.$http.delete(`goods/${goods_id}`)
      if(res.meta.status !== 200){
        return this.$message.error("请求服务器失败，无法删除")
      }
      this.$message.success("删除成功")
      this.getGoodsList()
    },
    //添加商品
    addGoods(){
        this.$router.push('/goods/add')
    }
  },

  created() {
    this.getGoodsList()
  },
}
</script>

<style>
</style>